package org.csu.petserver.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.petserver.domain.Category;
import org.csu.petserver.domain.Item;
import org.csu.petserver.domain.Product;
import org.csu.petserver.persistence.ItemMapper;
import org.csu.petserver.persistence.ProductMapper;
import org.csu.petserver.service.ItemInfo;
import org.csu.petserver.service.ProductInfo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;

@MapperScan("org.csu.petserver.persistence")
@Repository("ProductInfo")
@Service
public class ProductInfoImpl implements ProductInfo {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Qualifier("ItemInfo")
    @Autowired
    private ItemInfo itemInfo;

    @Override
    public List<Product> searchProductInCertainCategory(Category category, String keyword) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category.getId())
                .like("productid",keyword)
                .or().like("name",keyword);
        List<Product> products = productMapper.selectList(queryWrapper);
        return products;
    }

    @Override
    public List<Product> searchProductInAllCategory(String keyword) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("productid",keyword)
                .or().like("name",keyword);
        List<Product> products = productMapper.selectList(queryWrapper);
        return products;
    }

    @Override
    public List<Item> getAllItemByProduct(Product product) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",product.getId());
        List<Item> items = itemMapper.selectList(queryWrapper);
        return items;
    }

    @Override
    public List<Item> getAllItemByProductId(String productId) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",productId);
        return itemMapper.selectList(queryWrapper);
    }

    @Override
    public boolean addProduct(Product product) {
        productMapper.insert(product);
        return true;
    }

    @Override
    public boolean updateProduct(Product product) {
        productMapper.updateById(product);
        return true;
    }

    @Override
    public boolean deleteProduct(Product product) {
        productMapper.deleteById(product);
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid",product.getId());
        List<Item> items = itemMapper.selectList(queryWrapper);
        for (Item item: items) {
            itemInfo.deleteItem(item);
        }
        return true;
    }
    public List<Item> getAllItem(){
        return itemMapper.selectList(null);
    }

    public Product getProductById(String productId){
        return productMapper.selectById(productId);
    }
}
